package com.foreknow.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.RegistLevel;
import com.foreknow.service.RegistLevelService;
import com.foreknow.utils.Constance;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
@Api(tags = "RegisteLevelController", description = "挂号级别管理")
@RestController
@RequestMapping(value = "/RegisteLevelController")
public class RegisteLevelController {
    @Autowired
    private RegistLevelService levelService;
    @ApiOperation("获取挂号级别列表")
    @RequestMapping(value = "/quetyAll",method = RequestMethod.GET)
    public String quetyAll(@RequestParam Map<String,Object> para) throws JsonProcessingException {
        //获取模糊查询需要的参数
        String param = (String)para.get("query");
        RegistLevel r = new RegistLevel();
        r.setRegistname(param);
        r.setRegistcode(param);
        //获取分页需要的参数
        Integer pageNum =Integer.parseInt((String) para.get("pageNum"));
        Integer pageSize=Integer.parseInt((String) para.get("pageSize"));
        //设置分页信息
        PageHelper.startPage(pageNum,pageSize);

        List<RegistLevel> list = levelService.quetyAll(r);
        //调用PageInfo
        PageInfo<RegistLevel> info = new PageInfo<>(list);

        HashMap<String, Object> hs = new HashMap<>();
        //给HashMap添加数据
        hs.put("totalpage",info.getTotal());
        hs.put("pagenum",info.getPageNum());
        hs.put("data", list);
        if (list.size()>0) {//如果获取到数据
            hs.put("meta", new Constance("msg", 200));
        } else {
            hs.put("meta", new Constance("msg", 404));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/findRegistById",method = RequestMethod.GET)
    public String findRegistById(@RequestParam Integer id) throws JsonProcessingException {
        RegistLevel registLevel = levelService.queryById(id);
        HashMap<String, Object> hs = new HashMap<>();
        hs.put("data",registLevel);
        if (registLevel!=null){
            hs.put("meta",new Constance("msg",200));
        }else {
            hs.put("meta",new Constance("msg",403));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "/insertRegiste",method = RequestMethod.POST)
    public String insertRegiste(@RequestBody Map<String,Object> para)throws JsonProcessingException {
        String registCode = (String)para.get("registcode");
        String registName = (String)para.get("registname");
        Integer sequenceNo =Integer.parseInt((String) para.get("sequenceno"));
        Integer registeFee =Integer.parseInt((String) para.get("registfee"));
        Integer registQuota =Integer.parseInt((String) para.get("registquota"));

        RegistLevel registLevel = new RegistLevel();
        registLevel.setRegistcode(registCode);
        registLevel.setRegistname(registName);
        registLevel.setSequenceno(sequenceNo);
        BigDecimal bigDecimal = BigDecimal.valueOf(registeFee);
        registLevel.setRegistfee(bigDecimal);
        registLevel.setRegistquota(registQuota);

        int i = levelService.insertUser(registLevel);
        HashMap<String,Object> hs=new HashMap<>();
        hs.put("data",i);
        if (i>0){
            hs.put("meta",new Constance("添加用户成功",200));
        }else {
            hs.put("meta",new Constance("添加用户失败",403));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "/deleteRegisteById",method = RequestMethod.GET)
    public String deleteRegisteById(@RequestParam Map<String,Object> para)throws JsonProcessingException {
        Integer id =Integer.parseInt((String) para.get("id"));
        HashMap<String,Object> hs=new HashMap<>();
        int i = levelService.deleteById(id);
        hs.put("data",i);
        if (i>0){
            hs.put("meta",new Constance("删除用户成功",200));
        }else {
            hs.put("meta",new Constance("删除用户失败",403));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/deleteRegiste",method = RequestMethod.POST)
    public String deleteRegiste(@RequestBody Map<String,List<Integer>>ids) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        Integer registeByid = levelService.deleteRegisteByid(ids.get("list"));
        if(registeByid>0){
            hs.put("meta", new Constance("删除成功！"+registeByid+"条数据被删除", 200));
        }else{
            hs.put("meta", new Constance("删除失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "/modify",method = RequestMethod.PUT)
    public String modifyRegite(@RequestBody RegistLevel user) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        int i = levelService.updateById(user);
        hs.put("data",i);
        if (i>0){
            hs.put("meta", new Constance("修改成功", 200));
        }else {
            hs.put("meta", new Constance("修改失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
//    @RequestMapping(value = "/findregistelevel",method = RequestMethod.GET)
//    public List<RegistLevel> findAllReg(){
//        return  levelService.quetyAll("");
//    }
}
